Ext.define('Module.task.Taskxs', {
	extend : 'Ext.panel.Panel',
	layout: 'fit',
	border: false,
	initComponent : function() {
		var me = this;
		var _fields = ['id','transformer_id','type','dept_id','bz_id','xs_user_id','voltage_grade',
           'starttime','endtime','weather','temperature','defect_number','hdanger_number',
           'isfinish','nofinish_why','content','major_id','file','syntime','uupuser',
           'uptime','enable','bdz_code','bdz_name','dept','xsuser','bz_code','bz_name','city',
           'bzzy_code','bzzy_name','transformer','line','xsuser','result','region','zylx'];
		var store = Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields:_fields,
			proxy:{
				type:'ajax',
				url:'taskxs/list.do',
				reader:{
					root: 'datas',
					totalProperty: 'total'
				}
			}
		});
		var grid  = me.grid= Ext.create("Ext.grid.Panel",{
			listeners:{
				afterrender:function(){
					grid.down('field[name=startTime]').setValue();
					grid.down('field[name=endTime]').setValue();
				},
				itemdblclick:function(s,rec,item){
		    		Ext.create('Module.task.TaskDetails',{
		    			type: 2,
		    			rwdata: rec.raw
		    		}).show();
		    	}
			},
			title:'巡视任务管理',
			tbar:{
				xtype:'form',
				bodyStyle:{'background-color':'rgb(219, 242, 239)'},
				layout:'absolute',
				height:110,
				items:[{
					x: 5,
					y: 80,
					xtype:'button',
					text:'添加任务',
					iconCls:'icon-add',
					handler:function(){
						me.edit();
					}
				},{
					x: 90,
					y: 80,
					xtype:'button',
					text:'修改任务',
					iconCls:'icon-edit',
					handler:function(){
						var rec = grid.getSelectionModel().getSelection();
						if(rec.length > 0){
							me.edit(rec[0]);
						}else{
							Ext.Msg.alert('提示', '请选择要修改的数据！');
						}
					}
				},{
					x: 175,
					y: 80,
					xtype:'button',
					text:'删除任务',
					iconCls:'icon-delete',
					handler:function(){
						var rec = grid.getSelectionModel().getSelection()[0];
						if(rec){
							me.del(rec);
						}else{
							Ext.Msg.alert('提示', '请选择要删除的数据！');
						}
					}
				},{
					x:260,
					y: 80,
					xtype:'button',
					text:'Excel导入',
					iconCls:'icon-File_XLS_Excel',
					handler:function(){
						var fp = Ext.create('Ext.window.Window',{
							width:400,
							height:103,
							modal:true,
							buttonAlign: 'center',
							items:[{
								xtype:'form',
								bodyPadding: 10,
								border: false,
								items:[{  
									xtype: 'fileuploadfield',  
									emptyText: '请选择导入文件',  
									fieldLabel: '',  
									name: 'file',  
									buttonText: '选择文件',  
									anchor:'100%',
									bodyPadding: 10,
									allowBlank: false,
									buttonCfg: {  
										iconCls: 'upload-icon'  
									}  
								}]
							}],
							buttons:[{
								text:'导入',
								handler:function(){
									var _form = fp.down('form').getForm();
									if(_form.isValid()){
										_form.submit({
											url:'taskxs/impExcel.do',
											waitMsg: '请稍等，正在上传数据...',
											success: function(form, action) {
												var rs = Ext.decode(action.response.responseText);
												if(rs.rst){
													fp.close();
													Ext.Msg.alert('提示', rs.msg);
													me.grid.store.load();
												}
												else{
													Ext.Msg.alert('提示', rs.msg);
												}
											}
										});
									}
								}
							},{
								text:'取消',
								handler:function(){
									fp.close();
								}
							}]
						}).show();
					}
				},{
					xtype: 'button',
					text:'多角度视频回放',
					x: 350,
					y: 80,
					iconCls:'icon-play',
					handler:function(){
						var rec = grid.getSelectionModel().getSelection()[0];
						if(rec){
							Ext.create('Module.task.MultiVideoPlay',{
								rwdata: rec.data,
								rwtype: 2
							}).show();
						}else{
							Ext.Msg.alert('提示', '请选择任务!');
						}
					}
				},{
					xtype: 'button',
					text:'查看详情',
					x: 470,
					y: 80,
					iconCls:'icon-grid',
					handler:function(){
						var rec = grid.getSelectionModel().getSelection()[0];
						if(rec){
							Ext.create('Module.task.TaskDetails',{
				    			type: 2,
				    			rwdata: rec.raw
				    		}).show();
						}else{
							Ext.Msg.alert('提示', '请选择任务!');
						}
					}
				},{
					xtype: 'button',
					text:'实时视频',
					x: 555,
					y: 80,
					iconCls:'icon-play',
					handler:function(){
						var rec = grid.getSelectionModel().getSelection()[0];
						if(rec){
							Ext.Ajax.request({
							    url: 'test/exec.do',
							    success: function(response,action){
							    	var rs = Ext.decode(response.responseText);
							    	if(!rs.rst){
							    		Ext.Msg.alert('提示', rs.msg);
							    	}
							    }
							});
						}else{
							Ext.Msg.alert('提示', '请选择任务!');
						}
					}
				},{
					x:29,
					y: 5,
					labelWidth:55,
					width:Ext.isIE8 ? 212: (Ext.isIE ? 212: 225),
					fieldLabel: '电压等级',
					maxLength:16,
					xtype:'textfield'
				},{
					x:315,
					y: 5,
					labelWidth:45,
					xtype:'combobox',
					fieldLabel: '变电站',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'worksy/getBdz.do'
						}
					})
				},{
					x:604,
					y: 5,
					labelWidth:36,
					width:Ext.isIE8 ? 210: (Ext.isIE ? 218: 205),
					xtype:'combobox',
					name:'major_id',
					fieldLabel: '专业',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'worksy/getzy.do'
						}
					}),
					listeners:{
						select:function(c){
							c.up('form').down('field[fieldLabel=巡视班组]').setValue();
						}
					}
				},{
					x:830,
					y: 5,
					labelWidth:64,
					width:Ext.isIE8 ? 225: (Ext.isIE ? 225: 225),
					xtype:'combobox',
					fieldLabel: '巡视班组',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'worksy/getBz.do'
						}
					}),
					listeners: {
						afterrender: function(c){
							var _zy = c.up('form').down('field[fieldLabel=专业]');//.getSubmitValue().trim(),
							c.addManagedListener(_zy,'select',function(){
								c.store.load({params:{
									zyId: _zy.getSubmitValue()
								}});
							});
						}
					}	
				},{
					x:5,
					y: 30,
					labelWidth:80,
					fieldLabel: '巡视是否完成',
					name: 'isfinish',
					xtype: 'combobox',
					store: {
						xtype: 'store',
						fields: ['v', 'p'],
						data : [
					        {v: 0,p: '否'},
					        {v: 1,p: '是'}
						]
					},
					queryMode: 'local',
					displayField: 'p',
					valueField: 'v'
				},{
					x:280,
					y: 30,
					labelWidth:80,
					fieldLabel: '巡视是否归档',
					name: 'file',
					xtype: 'combobox',
					store: {
						xtype: 'store',
						fields: ['v', 'p'],
						data : [
					        {v: 0,p: '否'},
					        {v: 1,p: '是'}
						]
					},
					queryMode: 'local',
					displayField: 'p',
					valueField: 'v'
				},{
					x:580,
					y: 30,
					labelWidth:60,
					width:Ext.isIE8 ? 210: (Ext.isIE ? 218: 229),
					fieldLabel: '巡视类型',
					maxLength:16,
					xtype:'textfield'
				},{
					x:830,
					y: 30,
					labelWidth:64,
					width:Ext.isIE8 ? 225: (Ext.isIE ? 225: 225),
					fieldLabel: '巡视内容',
					maxLength:120,
					xtype:'textfield'
				},{
					x:5,
					y: 55,
					name: 'startTime',
					fieldLabel: '巡视开始时间',
					labelWidth:80,
					xtype:'datefield',
					format:'Y-m-d H:i:s',
					//editable:false,
					value:Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,0),"Y-m-d H:i:s")
				},{
					x:280,
					y: 55,
					name: 'endTime',
					fieldLabel: '巡视结束时间',
					labelWidth:80,
					xtype:'datefield',
					format:'Y-m-d H:i:s',
					//editable:false,
					value:Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,0),"Y-m-d H:i:s")
				},{
					xtype:'button',
					x:540,
					y: 55,
					text:'查询',
					width: 55,
					iconCls: 'icon-search',
					handler:function(){
						var startTime = grid.down('field[name=startTime]').getSubmitValue();
						var endTime = grid.down('field[name=endTime]').getSubmitValue();
						if(new Date(startTime) > new Date(endTime)){
							Ext.Msg.alert('提示', "巡视开始时间大于巡视结束时间，请重新选择！");
							return;
						}
						dydj = grid.down('field[fieldLabel=电压等级]').getSubmitValue();
						type = grid.down('field[fieldLabel=巡视类型]').getSubmitValue();
						content = grid.down('field[fieldLabel=巡视内容]').getSubmitValue();
						if(dydj.length > 16 || type.length > 16 || content.length > 120) return;
						
						grid.store.proxy.extraParams = {
							dydj: grid.down('field[fieldLabel=电压等级]').getSubmitValue(),
							bdz: grid.down('field[fieldLabel=变电站]').getSubmitValue(),
							bz: grid.down('field[fieldLabel=巡视班组]').getSubmitValue(),
							zy: grid.down('field[fieldLabel=专业]').getSubmitValue(),
							startTime:grid.down('field[name=startTime]').getSubmitValue(),
							endTime:grid.down('field[name=endTime]').getSubmitValue(),
							isfinish:grid.down('field[fieldLabel=巡视是否完成]').getSubmitValue(),
							file:grid.down('field[fieldLabel=巡视是否归档]').getSubmitValue(),
							type:grid.down('field[fieldLabel=巡视类型]').getSubmitValue(),
							content:grid.down('field[fieldLabel=巡视内容]').getSubmitValue()
						};
						grid.store.loadPage(1);
					}
				},{
					xtype:'button',
					x:600,
					y: 55,
					text:'重置',
					width: 35,
					handler:function(){
						grid.down('field[fieldLabel=电压等级]').setValue();
						grid.down('field[fieldLabel=变电站]').setValue();
						grid.down('field[fieldLabel=巡视班组]').setValue();
						grid.down('field[fieldLabel=专业]').setValue();
						grid.down('field[name=startTime]').setValue();
						grid.down('field[name=endTime]').setValue();
						grid.down('field[fieldLabel=巡视是否完成]').setValue();
						grid.down('field[fieldLabel=巡视是否归档]').setValue();
						grid.down('field[fieldLabel=巡视类型]').setValue();
						grid.down('field[fieldLabel=巡视内容]').setValue();
					}
				}]
			},
			bbar: Ext.create('Ext.PagingToolbar', {
	            store: store,
	            displayInfo: true,
	            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
	        }),
			store:store,
			columns:[{
				text: 'ID',
				dataIndex: 'id',
				width: 80,
				hidden: true
			},{
				text: '电压等级',
				dataIndex: 'voltage_grade',
				width: 120,
			},{
				text: '变电站',
				dataIndex: 'transformer',
				width: 150
			}/*,{
				text: '变电站编码',
				dataIndex: 'bdz_code',
				width: 120,
				hidden:true
			},{
				text: '电站|线路',
				dataIndex: 'line',
				width: 110,
			}*/,{
				text: '巡视类型',
				dataIndex: 'type',
				width: 100,
			},{
				text: '巡视内容',
				dataIndex: 'content',
				width: 250
			},{
				text: '地市单位',
				dataIndex: 'city',
				width: 120
			},{
				text: '巡视班组',
				dataIndex: 'bz_name',
				width: 120
			},{
				text: '班组编码',
				dataIndex: 'bz_code',
				width: 120,
				hidden:true
			},{
				text: '专业',
				dataIndex: 'bzzy_name',
				width: 120
			},{
				text: '专业编码',
				dataIndex: 'bzzy_code',
				width: 120,
				hidden:true
			},{
				text: '巡视人员',
				dataIndex: 'xsuser',
				width: 150
			},{
				text: '巡视开始时间',
				dataIndex: 'starttime',
				width: 130,
				renderer: formatDate
			},{
				text: '巡视结束时间',
				dataIndex: 'endtime',
				width: 130,
				renderer: formatDate
			},{
				text: '天气',
				dataIndex: 'weather',
				width: 80
			},{
				text: '气温',
				dataIndex: 'temperature',
				width: 80
			},{
				text: '发现缺陷数',
				dataIndex: 'defect_number',
				width: 100,
				regex:/^1[34578]\d{9}$/,
				maxLength:11,
				minLength:11,
				minValue:1
			},{
				text: '发现隐患数',
				dataIndex: 'hdanger_number',
				width: 100,
				regex:/^1[34578]\d{9}$/,
				maxLength:11,
				minLength:11,
				minValue:1
			},{
				text: '巡视是否完成',
				dataIndex: 'isfinish',
				width: 100,
				renderer:function(v){
					return v == 1?'是':'否';
				}
			},{
				text: '巡视未完成原因',
				dataIndex: 'nofinish_why',
				width: 150
			},{
				text: '巡视结果',
				dataIndex: 'result',
				width: 250
			},{
				text: '是否归档',
				dataIndex: 'file',
				width: 100,
				renderer:function(v){
					return v == 1?'是':'否';
				}
			},{
				text: '同步时间',
				dataIndex: 'syntime',
				width: 130,
				renderer: formatDate
			},{
				text: '更新用户',
				dataIndex: 'uupuser',
				width: 120
			},{
				text: '更新时间',
				dataIndex: 'uptime',
				width: 130,
				renderer: formatDate
			}]
		});

		me.items = grid;
		me.callParent();
	},
	edit:function(rec){
		var me = this;
		Ext.create("Ext.window.Window",{
			width:500,
			height:590,
			modal:true,
			iconCls: rec ? 'icon-edit' : 'icon-add',
			title: rec ? '修改巡视任务' : '添加巡视任务',
			buttons:[{
				text:'保存',
				handler:function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.form;
        	    	var o = form.getValues();
        	    	if(new Date(o.starttime) > new Date(o.endtime)){
        	    		Ext.Msg.alert('提示', "巡视开始时间大于巡视结束时间，请重新选择！");
        	    		return;
        	    	}
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'taskxs/edit.do',
            	    	    success: function(form, action) {
            	    	       var rs = Ext.decode(action.response.responseText);
    	       	    	    	if(rs.rst){
    	       	    	    		Ext.Msg.alert('提示', rs.msg);
    	       	    	    		formp.up('window').close();
    	       	    	    		me.grid.store.load();
    	       	    	    	}
    	       	    	    	else{
    	       	    	    		Ext.Msg.alert('提示', '保存失败！');
    	       	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '请填写正确的数据信息。');
        	    	}
				}
			},{
				text:'取消',
				handler:function(){
					this.up('window').close();
				}
			}],
			items:me.getForm({
				bodyPadding: 20,
				border: false,
				defaults: {anchor: '100%'},
				listeners: {
					afterrender: function(formp){
						if(rec){
							formp.loadRecord(rec||{});
							formp.form.setValues({
								syntime: new Date(rec.data.syntime),
								endtime: new Date(rec.data.endtime),
								starttime: new Date(rec.data.starttime)
							});
						}
					}
				}
			},rec)
		}).show();
	},
	del:function(rec){
		var me = this;
			Ext.Msg.confirm('提示', '删除该数据后将不再关联视频，你确定要删除当前数据吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'taskxs/del.do',
					    params: {
					        id: rec.data.id,
					        content:rec.data.content
					    },
					    success: function(response){
					    	var rs = Ext.decode(response.responseText);
					    	Ext.Msg.alert('提示', rs.msg);
					    	me.grid.store.load();
					    }
					});
				}
			});
	},
	getForm:function(config,rec){
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {allowBlank: false},
			items: [{
				xtype: 'hidden',
				name: 'id'
			},{
				xtype: 'textfield',
				name: 'voltage_grade',
				fieldLabel: '电压等级',
				maxLength:30,
				regex:/^[\u4E00-\u9FA5A-Za-z0-9]+$/,
				regexText:'不能输入特殊字符',
				allowBlank: false
			}/*,{
				xtype: 'textfield',
				name: 'line',
				fieldLabel: '电站|等级',
				maxLength:70,
				allowBlank: false
			}*/,{
				xtype:'hidden',
				name:'transformer'
			},{
				allowBlank: false,
				xtype:'combobox',
				name:'region',
				fieldLabel: '地市单位',
				readOlay:true,
				displayField: 'name',
				valueField: 'id',
				queryMode: 'local',
				store: Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields: [{name:'id',type:'string'},'name'],
					proxy: {
						type: 'ajax',
						url: 'worksy/getCity.do'
					}
				}),
				listeners:{
					select:function(c){
						c.up('form').down('combobox[name=transformer_id]').setValue();
					}
				}
			},{
				allowBlank: false,
				xtype:'combobox',
				name:'transformer_id',
				fieldLabel: '变电站',
				readOlay:true,
				displayField: 'name',
				valueField: 'id',
				queryMode: 'local',
				store: Ext.create('Ext.data.Store',{
					autoLoad: false,
					fields: ['id','name'],
					proxy: {
						type: 'ajax',
						url: 'worksy/getBdz.do'
					}
				}),
				listeners:{
					boxready: function(c){
						c.store.load({params:{
							regionId: c.up('form').down('combobox[name=region]').getValue()
						}});
						var region = c.up('form').down('combobox[name=region]');
						c.addManagedListener(region,'select',function(){
							c.store.load({params:{
								regionId: region.getSubmitValue()
							}});
						});
					},
					change:function(f){
						f.up('form').form.setValues({
							transformer:f.getRawValue()
						});
					}
				}
			},{
				allowBlank: false,
				xtype:'combobox',
				name:'major_id',
				fieldLabel: '专业',
				readOlay:true,
				displayField: 'name',
				valueField: 'id',
				queryMode: 'local',
				store: Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields: ['id','name'],
					proxy: {
						type: 'ajax',
						url: 'worksy/getzy.do'
					}
				}),
				listeners:{
					select:function(c){
						c.up('form').down('combobox[name=bz_id]').setValue();
					}
				}
			},{
				allowBlank: false,
				xtype:'combobox',
				name:'bz_id',
				fieldLabel: '巡视班组',
				readOlay:true,
				displayField: 'name',
				valueField: 'id',
				queryMode: 'local',
				store: Ext.create('Ext.data.Store',{
					autoLoad: false,
					fields: ['id','name'],
					proxy: {
						type: 'ajax',
						url: 'worksy/getBz.do'
					}
				}),
				listeners: {
					boxready: function(c){
						var _zy = c.up('form').down('combobox[name=major_id]');
						c.store.load({params:{
							zyId: _zy.getSubmitValue()
						}});
						c.addManagedListener(_zy,'select',function(){
							c.store.load({params:{
								zyId: _zy.getSubmitValue()
							}});
						});
					}
				}
			},{
				xtype: 'textfield',
				name: 'xsuser',
				fieldLabel: '巡视用户',
				maxLength:70,
				allowBlank: false
			},{
				xtype: 'textfield',
				name: 'type',
				fieldLabel: '巡视类型',
				maxLength:30,
				allowBlank: false
			},{
				xtype: 'textfield',
				name: 'weather',
				fieldLabel: '天气',
				maxLength:30,
				allowBlank: false
			},{
				xtype: 'numberfield',
				name: 'temperature',
				fieldLabel: '气温',
				maxLength:15,
				allowBlank: false
			},{
				xtype: 'numberfield',
				name: 'defect_number',
				fieldLabel: '发现缺陷数',
				minValue:0,
				allowBlank: false,
				value:0
			},{
				xtype: 'numberfield',
				name: 'hdanger_number',
				fieldLabel: '发现隐患数',
				minValue:0,
				allowBlank: false,
				value:0
			},{
				name: 'isfinish',
				fieldLabel: '巡视是否完成',
				xtype: 'combobox',
				store: {
					xtype: 'store',
					fields: ['isfinish', 'p'],
					data : [
				        {isfinish: 0,p: '否'},
				        {isfinish: 1,p: '是'}
					]
				},
				queryMode: 'local',
				displayField: 'p',
				valueField: 'isfinish',
				allowBlank: false,
				listeners:{
					boxready:function(c){
						if(c.value == 1){
							 c.up('form').down('field[name=nofinish_why]').hide();
						}
					}
				}
			},{
				xtype: 'textfield',
				name: 'nofinish_why',
				fieldLabel: '巡视未完成原因',
				maxLength:150,
				listeners:{
					afterrender:function(c){
						var isfinish = c.up('form').down('field[name=isfinish]');
						c.addManagedListener(isfinish,'select',function(){
							if(isfinish.getSubmitValue() == '是' || isfinish.getSubmitValue() == 1){
								c.hide();
								c.setValue();
							}else{
								c.show();
							}
						});
					}
				}
			},{
				xtype: 'textfield',
				name: 'content',
				maxLength:300,
				fieldLabel: '巡视内容',
				allowBlank: false
			},{
				xtype: 'textfield',
				name: 'result',
				fieldLabel: '巡视结果',
				maxLength:150,
				allowBlank: false
			},{
				name: 'file',
				fieldLabel: '是否归档',
				xtype: 'combobox',
				store: {
					xtype: 'store',
					fields: ['file', 'p'],
					data : [
				        {file: 0,p: '否'},
				        {file: 1,p: '是'}
					]
				},
				queryMode: 'local',
				displayField: 'p',
				valueField: 'file',
				allowBlank: false
			},{
				name: 'starttime',
				fieldLabel: '巡视开始时间',
				xtype:'datefield',
				format:'Y-m-d H:i:s',
				editable:true,
				//value:Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,0),"Y-m-d H:i:s"),
				allowBlank: false	
			},{
				name: 'endtime',
				fieldLabel: '巡视结束时间',
				xtype:'datefield',
				format:'Y-m-d H:i:s',
				editable:true,
				//value:Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,0),"Y-m-d H:i:s"),
				allowBlank: false
			},{
				allowBlank: false,
				xtype:'combobox',
				name:'zylx',
				fieldLabel: '作业类型',
				displayField: 'name',
				valueField: 'id',
				queryMode: 'local',
				store: Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields: ['id','name'],
					proxy: {
						type: 'ajax',
						url: 'bzzy/BzzyZygcAction/getJcZyType.do'
					}
				})
			}]
		},config);
	}
});